<template>
  <div class="myDiv">
    <tou title="用户详情"></tou>
    <van-tabs v-model="activeName" color="#409eff">
      <van-tab :title="isadd?'添加用户':'编辑用户'" name="a">
        <div>
          <van-field v-model.trim="addobj.name" label="姓名:" required :readonly="isset" />
          <van-radio-group v-model="addobj.gender" :disabled="isset">
            <van-cell-group>
              <van-cell required title="性别：">
                <van-radio slot="right-icon" name="1" style="margin-left: 35px;">男&nbsp;&nbsp;&nbsp;</van-radio>
                <van-radio slot="right-icon" name="2">女</van-radio>
              </van-cell>
            </van-cell-group>
          </van-radio-group>
          <van-field
            v-model="addobj.birthday"
            label="出生日期："
            required
            :readonly="isset"
            @click="showday"
          />
          <van-field v-model="addobj.idcardno" type="number" label="身份证号:" :readonly="isset" />
          <van-field
            v-model.trim="addobj.phone"
            type="number"
            required
            label="手机号码："
            :readonly="isset"
          />
          <van-field v-model="addobj.email" label="邮箱：" :readonly="isset" />
        </div>
        <div style="border-top: 1px solid #F1F4F8;" v-if="!isadd&&isset">
          <van-field v-model="msg.age" label="年龄：" :readonly="isset" />
          <van-field v-model="msg.orgname" label="医生：" :readonly="isset" />
          <van-field v-model="msg.addtime" label="注册时间：" :readonly="isset" />
        </div>
        <van-cell size="large" class="btns" v-if="!isadd">
          <van-button size="small" type="primary" v-if="isset" @click="setmsg">编辑</van-button>
          <van-button size="small" type="primary" v-if="!isset" @click="isset=true">取消</van-button>
          <van-button size="small" type="primary" v-if="!isset" @click="sure">确认修改</van-button>
          <van-button size="small" type="danger" v-if="isset" @click="del">删除</van-button>
        </van-cell>
        <van-cell size="large" class="btns" v-if="isadd">
          <van-button size="small" type="primary" v-if="isset||isadd" @click="add(1)">取消</van-button>
          <van-button size="small" type="primary" v-if="isset||isadd" @click="add(2)">确认添加</van-button>
        </van-cell>
      </van-tab>
      <!-- <van-tab title="添加订单" v-if="!this.isadd" name="b">
        <van-field
          required
          label-width="70px"
          v-model="used"
          label="体检标识"
          @click="show1=true"
          placeholder="请选择"
          readonly
        />
        <van-field
          required
          label-width="70px"
          v-model="state"
          label="支付状态"
          @click="show2=true"
          placeholder="请选择"
          readonly
        />
        <van-field
          required
          label-width="70px"
          v-model="deviceNo"
          label="体检设备"
          @click="show3=true"
          placeholder="请选择"
          readonly
        />
        <van-field
          required
          label-width="70px"
          v-model="types"
          label="检查类型"
          @click="show4=true"
          placeholder="请选择"
          readonly
        />
        <div class="btns">
          <van-button size="small" type="primary" @click="addOrder" v-if="isset">添加订单</van-button>
        </div>
      </van-tab> -->
    </van-tabs>
    <!-- <van-action-sheet
      v-model="show1"
      close-on-click-action
      :actions="actions1"
      @select="onSelect1"
    />
    <van-action-sheet
      v-model="show2"
      close-on-click-action
      :actions="actions2"
      @select="onSelect2"
    />
    <van-action-sheet
      v-model="show3"
      close-on-click-action
      :actions="actions3"
      @select="onSelect3"
    />
    <van-action-sheet
      v-model="show4"
      close-on-click-action
      :actions="actions4"
      @select="onSelect4"
    /> -->
    <!-- 日期 -->
    <van-popup v-model="showdate" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        @confirm="suredate"
        @cancel="showdate=false"
        :max-date="maxDate"
        :min-date="minDate"
      />
    </van-popup>
  </div>
</template>
    
    <script>
import tou from "@/components/header";
export default {
  components: { tou },
  props: {},
  data() {
    return {
      msg: "",
      isset: true, //是否禁用输入框
      isadd: false, //是否通过添加进来
      show1: false,
      show2: false,
      show3: false,
      show4: false,
      showdate: false,
      error: "", //必填提示
      currentDate: new Date(1980, 10, 1),
      minDate: new Date(1920, 10, 1),
      maxDate: new Date(),
      actions1: [
        { name: "未体检", id: 0 },
        { name: "体检开始", id: 1 },
        { name: "体检结束", id: 2 }
      ], //下拉框信息
      actions2: [{ name: "已付款", id: 1 }],
      actions3: [], //体检设备集合
      actions4: [], //检查类型集合
      activeName: "a",
      addobj: {
        keyid: "",
        name: "",
        gender: "1",
        birthday: "",
        idcardno: "",
        phone: "",
        email: ""
      },
      adddd: {
        userid: "",
        username: "",
        sex: "",
        birthday: "",
        orderPhone: "",
        used: "",
        state: "",
        deviceNo: "",
        types: "",
        body: "天探体检",
        orderPayment: "系统录入"
      },
      used: "",
      state: "",
      deviceNo: "",
      types: ""
    };
  },
  watch: {},
  computed: {},
  methods: {
    setmsg() {
      this.isset = false;
    },
    showday() {
      if (!this.isset) {
        this.showdate = true;
      } else {
        this.showdate = false;
      }
    },
    //添加订单
    // addOrder() {
    //   if (this.used == "") {
    //     this.Tips("请选择体检标识", "warning");
    //   } else if (this.state == "") {
    //     this.Tips("请选择支付状态", "warning");
    //   } else if (this.deviceNo == "") {
    //     this.Tips("请选择体检设备", "warning");
    //   } else if (this.types == "") {
    //     this.Tips("请选择检查类型", "warning");
    //   } else {
    //     this.$http(this.api.addOrder, this.adddd).then(res => {
    //       if (res.status === 200) {
    //         if (res.data.result == "true") {
    //           this.Tips(res.data.message, "success");
    //           this.$router.replace("/device/order");
    //         } else {
    //           this.Tips(res.data.message, "warning");
    //         }
    //       }
    //     });
    //   }
    // },
    sure() {
      //更新用户信息
      if (this.addobj.name == "") {
        this.Tips("请输入姓名！", "warning");
      } else if (this.addobj.birthday == "") {
        this.Tips("请选择出生日期！", "warning");
      } else if (this.addobj.phone == "") {
        this.Tips("请输入手机号码！", "warning");
      } else {
        this.$http(this.api.updatePatientInfo, this.addobj).then(res => {
          if (res.status === 200) {
            if (res.data.result == "true") {
              this.Tips(res.data.message, "success");
              // this.$router.go(-1)
              this.isset = true;
            } else {
              this.Tips(res.data.message, "warning");
            }
          }
        });
      }
    },
    del() {
      this.$dialog
        .confirm({
          title: "提示",
          message: "确定要删除吗"
        })
        .then(() => {
          this.$http(this.api.deletePatientInfo, {
            keyid: this.addobj.keyid
          }).then(res => {
            if (res.status === 200) {
              if (res.data.result == "true") {
                this.Tips(res.data.message, "success");
                this.$router.go(-1);
              } else {
                this.Tips(res.data.message, "warning");
              }
            }
          });
        })
        .catch(() => {});
    },
    add(a) {
      if (a == 1) {
        this.$router.go(-1);
      } else if (a == 2) {
        if (this.addobj.name == "") {
          this.Tips("请输入姓名！", "warning");
        } else if (this.addobj.birthday == "") {
          this.Tips("请选择出生日期！", "warning");
        } else if (this.addobj.phone == "") {
          this.Tips("请输入手机号码！", "warning");
        } else {
          this.$http(this.api.insertPatientInfo, this.addobj).then(res => {
            if (res.status === 200) {
              if (res.data.result == "true") {
                this.Tips(res.data.message, "success");
                this.$router.go(-1);
              } else {
                this.Tips(res.data.message, "warning");
              }
            }
          });
        }
      }
    },
    selectTypesBySexAndBuy(userkind) {
      this.$http(this.api.selectTypesBySexAndBuy, {
        gender: this.msg.gender,
        keyid: this.msg.keyid
      }).then(res => {
        if (res.status === 200) {
          if (res.data.result == "true") {
            if (userkind == 1) {
              res.data.pageData.data[0].dalist.filter((item, index) => {
                this.actions4.push({ name: item.value, id: item.name });
              });
            } else if (userkind == 2) {
              res.data.pageData.data[0].meallist.filter((item, index) => {
                this.actions4.push({
                  name: item.dictionaryName,
                  id: item.dictionaryId
                });
              });
            }
            this.adddd.userid = res.data.pageData.data[0].infomodel.guid;
            res.data.pageData.data[0].orglist.filter((item, index) => {
              this.actions3.push({ name: item.address, id: item.deviceNo });
            });
          } else {
          }
        }
      });
    },
    suredate(date) {
      this.showdate = false;
      this.addobj.birthday = this.setdate(date, "{y}-{m}-{d}");
    },
    onSelect1(item) {
      this.used = item.name;
      this.adddd.used = item.id;
    },
    onSelect2(item) {
      this.state = item.name;
      this.adddd.state = item.id;
    },
    onSelect3(item) {
      this.deviceNo = item.name;
      this.adddd.deviceNo = item.id;
    },
    onSelect4(item) {
      this.types = item.name;
      this.adddd.types = item.id;
    }
  },
  created() {
    this.msg = this.$route.query;
    this.isadd = this.$route.query.isadd;
    if (this.isadd == true) {
      this.isset = false;
    }
    if (this.msg.keyid) {
      this.addobj.keyid = this.msg.keyid;
      this.addobj.name = this.msg.name;
      this.adddd.username = this.msg.name;
      this.addobj.gender = String(this.msg.gender);
      this.adddd.sex = String(this.msg.gender);
      this.addobj.birthday = this.msg.birthday;
      this.adddd.birthday = this.msg.birthday;
      this.addobj.idcardno = this.msg.idcardno;
      this.addobj.phone = this.msg.phone;
      this.adddd.orderPhone = this.msg.phone;
      this.addobj.email = this.msg.email;
    }
  },
  mounted() {
    if (!this.$route.query.isadd) {
      this.selectTypesBySexAndBuy(this.$store.state.userinfo.userkind);
    }
  }
};
</script>
    <style scoped>
.myDiv {
}
.btns {
  width: max-content;
  margin-left: 50%;
  transform: translateX(-50%);
}
.title {
  color: #4ce8d3;
  font-size: 18px;
  text-align: center;
  line-height: 18px;
  margin: 10px 0;
}
.van-cell__title {
  flex: none !important;
  margin-right: 10px;
}
</style>